package com.zsh.utils;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtils {

    private static BasicDataSource dataSource = null;
    private static ThreadLocal<Connection> conns = new ThreadLocal<Connection>();

    static {


        try {

//            Properties properties = new Properties();
//            //读取jdbc.properties 配置文件
//            InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//            //从流中加载数据
//            properties.load(inputStream);
//            //创建数据库连接池
//            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
            InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            System.out.println(properties.getProperty("username"));

            //创建数据源 工厂模式-->创建对象
            dataSource = BasicDataSourceFactory.createDataSource(properties);

//            System.out.println(dataSource.getConnection());
        } catch (Exception e) {

        }
    }

    /**
     * 获取数据库池中的连接
     * 如果返回 null 则说明获取连接失败，有值就是成功
     * @return
     */
    public static Connection getConnection(){
        Connection conn = conns.get();
        if (conn == null){
            try {
                conn = dataSource.getConnection();  //从连接池里获取
                conns.set(conn); //保存到ThreadLocal中，供之后的JDBC使用
                conn.setAutoCommit(false); //设置手动管理事务
            } catch (SQLException e) {
                e.printStackTrace();
            }
         }

        return conn;
    }

    /**
     * 提交事务并关闭释放连接
     */
    public static void commitAndClose(){
        Connection connection = conns.get();
        if (connection != null){
            try {
                connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        conns.remove(); //用完之后就删去，tomcat底层使用了线程池
    }

    /**
     * 回滚事务并关闭释放连接
     */
    public static void rollbackAndClose(){
        Connection connection = conns.get();
        if (connection != null){
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        conns.remove(); //用完之后就删去，tomcat底层使用了线程池
    }


    /**
     * 关闭连接，放回数据库连接池
     * @param conn
     */
    public static void close(Connection conn){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //释放连接
    public static void releaseConnection(Connection connection, Statement statement, ResultSet resultset){
        if (resultset!=null){
            try {
                resultset.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}
